package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Category;
import com.itheima.bean.Dish;
import com.itheima.bean.Setmeal;
import com.itheima.common.CustomException;
import com.itheima.common.PageParm;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.DishDao;
import com.itheima.dao.SetmealDao;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    private DishDao dishDao;

    /**
     * 分页查询
     * @param parm
     * @return
     */
    @Override
    public IPage<Category> selectPage(PageParm parm) {
        IPage<Category> page = new Page<>(parm.getPage(),parm.getPageSize());
        return  categoryDao.selectPage(page,null);
    }

    /**
     * 插入分类
     * @param category
     * @return
     */
    @Override
    public int insert(Category category) {
        return  categoryDao.insert(category);
    }

    /**
     * 更新分类
     * @param category
     * @return
     */
    @Override
    public int updateById(Category category) {
        int i = categoryDao.updateById(category);
        return i;
    }

    @Override
    public int delete( Category category) {
        if(category.getType()==1){
            //查看当前分类菜品是否关联菜
            List<Dish> dishes = dishService.findByCategoryId(category.getId());
            if(dishes!=null&&dishes.size()!=0)
                //return -1;
                throw new CustomException("该分类下有关联的菜品");
        }
        else {
            List<Setmeal> setmeals = setmealService.findByCategoryId(category.getId());
            if(setmeals!=null&&setmeals.size()!=0){
                throw new CustomException("该分类下有关联的套餐");
            }
        }

        return categoryDao.deleteById(category.getId());


    }

    /**
     * 根据Type查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> selectByType(int type) {
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getType,type);
        List<Category> categories = categoryDao.selectList(qw);
        return categories;
    }

    @Override
    public Category selectById(long id) {
        Category category = categoryDao.selectById(id);
        return category;
    }

    @Override
    public List<Category> selectAll() {
        List<Category> categories = categoryDao.selectList(null);
        return categories;
    }


}
